package com.hehe.controller;

import com.hehe.Common.IPUtil;
import com.hehe.DTO.PhotoDTO;
import com.hehe.POJO.Photo;
import com.hehe.Repository.ArticleRepository;
import com.hehe.Repository.CollectRepository;
import com.hehe.Repository.PhotoRepository;
import com.hehe.Repository.UserRepository;
import com.hehe.Service.CollectService;
import com.hehe.Service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;

@Slf4j
@Controller
@RequestMapping(value = "")
public class IndexController {

    @Value("${config_file_name}")
    private String config_file_name;

    @Resource
    private ArticleRepository articleRepository;

    @Resource
    private UserRepository userRepository;

    @Resource
    private UserService userService;

    @Resource
    private CollectService collectService;

    @Resource
    private PhotoRepository photoRepository;

    @Resource
    private CollectRepository collectRepository;


    // 牛仔裤    迷你裙    超短裤     瑜伽裤  泳装   高跟鞋

    @GetMapping(value = "")      //    @GetMapping("/type/photo/{type}")
    public String index(HttpServletRequest request, Model model,
                        @RequestParam(defaultValue = "牛仔裤") String type,
                        @RequestParam(defaultValue = "1") int page,
                        @RequestParam(defaultValue = "8") int size) {

//        String requestIp = IPUtil.getRequestIp(request);
//        log.info("访问者IP地址为: {}", requestIp);

        Pageable pageable = PageRequest.of(page - 1, size);
        Page<Photo> content = photoRepository.findByTitleContaining(type, pageable);
        ArrayList<PhotoDTO> PhotoDTOList = new ArrayList<>();
        for (Photo photo : content.getContent()) {
            PhotoDTO photoDTO = new PhotoDTO();
            photoDTO.setId(photo.getId());
            photoDTO.setImgPath(photo.getImgPath().split(",")[1]);
            PhotoDTOList.add(photoDTO);
        }
        model.addAttribute("photoList", PhotoDTOList);
        model.addAttribute("type", type);
        return "index.html";
    }




}




